<template>
  <fml-dialog
    :addParam="addParam"
    :editParam="editParam"
    :editApi="editApi"
    :publicApi="publicApi"
    :dialogWidth="dialogWidth"
    :otherPublic="otherPublic"
    :addApi="addApi"
    :otherBtn="otherBtn"
    :otherParam="otherParam"
    :publicStatus='publicStatus'
    :formConfig="formConfig"
    ref="editDialog"
    title="公告">
  </fml-dialog>
</template>
<script>
import { mapState } from "vuex";
import { defaultConfig ,imgConfig} from "./dialog-config";
export default {
  name: "addEditDialog",
  data() {
    return {
      otherBtn:'发布',
      otherParam:{status:1},
      formConfig: {
        defaultConfig,
        fmlForm: {
          fileIds:[]
        }
      },
      dialogWidth: "450px",
      publicStatus:0,//未发布
      otherPublic:true,
      publicApi:'noticeList',
      editApi:'noticeList',
      addApi:'noticeList',
      editor: null
    };
  },
  computed: {
    ...mapState(["dialogVisible", "editData",'dialogType','userInfo']),
    initialFormValue() {
      return {
        title:'',
        context:'',
        fileIds:[],
        releaseName:this.userInfo.name
      };
    },
    addParam() {
      let param={
        ...this.formConfig.fmlForm,
        fileIds:this.$store.state.fileIds.join(',')
      };
      delete param['releaseName']
      return param
    },
    editParam() {
      let param = {
        id:this.addParam.id
      };
      return param;
    }
  },
  watch: {
    dialogVisible: {
      immediate: true,
      handler(v) {
        // 初始化和每次关闭弹窗是重置表单数据
        if (!v) {
          this.formConfig.fmlForm = { ...this.initialFormValue };
          this.formConfig.fmlForm.fileIds=[]
          this.$store.state.fileIds=[]
          this.formConfig.defaultConfig.tr[2].td[0].fileList=[]
        }else{
          this.$nextTick(()=>{
            this.formConfig.defaultConfig.tr[4].td[0].disabled=true
            if(!this.$refs.editDialog.$refs.fmlInput)return
            this.$refs.editDialog.$refs.fmlInput.$refs.input[0].focus()
          })
        }
      }
    },
    editData(v) {
      let fmlForm = this.formConfig.fmlForm;
      this.formConfig.defaultConfig.tr[2].td[0].filelist=[];
      if (v && Object.keys(v).length) {
        this.publicStatus=v.status
        this.$store.state.fileIds=v.fileIds.split(',')
        let file=v.files||[];
        if(file.length>0){
          for(var i=0;i<file.length;i++){
            this.formConfig.defaultConfig.tr[2].td[0].fileList.push({name:file[i].name+'.'+file[i].suffix,url:file[i].url,accept:file[i].suffix})
          }
        }
        const { title, context,fileIds,id} = v;
        // 不能直接将数据赋值给addEditForm，这会导致表单传入的form不再是原来的对象，从而使表单的全局方法，如resetFields失效
        Object.assign(fmlForm, {title, context,fileIds,id });
      }
    }
  }
};
</script>
